<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once('CBase.php');

class CI_CBook extends CBase {	

    var $page_size = 10;
    
    function __construct($p)
    {
        parent::__construct($p);
        
        if(!$this->validate_key()){
            $this->error('Authority Key unavailiable.');
        }        	
        
        $this->valid_login($this->req['login_key']);
        
        $this->CI->load->model('m_Book');
    }
    
    /*
     * Show data list
     * p={pageSize:?,pageIndex:?}
     */
    public function show_all(){
        $page_size = $this->req['pageSize'] * 1;
        $page = $this->req['pageIndex'] * 1;
        
        if(!$page_size) $page_size = $this->page_size;
        if(!$page) $page = 1;
        
        $where = array(
            'user_id'   => $this->user->user_id,
            'deleted'   => 0
        );
        
        $pager = $this->m_Book->get_pager($page_size,$where);
        if($pager['total']>0){
            $list = $this->m_Book->select_list($where,'event_time desc',
                    $page_size, $page * $this->page_size);

            $pager['list'] = $list;                    
        }        
        $this->show_msg ('success', $pager);
    }
    
    /*
     * Show single record
     * p={id:?}
     */
    public function show(){            
        //validate data
        $book_id = $this->req['id'] * 1;
        if($book_id<=0) $this->error('record id is incorrect.');
        
        //check the record is exists.              
        $where = array(
            'deleted'   => 0,
            'user_id'   => $this->user->user_id,
            'book_id'   => $book_id
        );
        $item = $this->CI->m_Book->select_one($where);        
        
        if(!$item)
            $this->error('recored is not found.');
        
        unset($item['deleted']);
        unset($item['user_id']);
        unset($item['username']);
        $this->show_msg ('success', $item);  
    }

    /*
     * Add new record
     * p={<bookObject>}
     */
    public function add()
    {
        $res = $this->validate();
        if($res['err'])
            $this->show_msg ('validate failed.', $res['err'], 0);
        
        if($this->CI->m_Book->insert($res['book'])){
            $this->show_msg ('success');
        }
        else
        {
            $this->error('insert failed');
        }        
    }
    
    /*
     * Edit a record
     * p={<bookObject>}
     */
    public function edit()
    {
        //validate data
        $res = $this->validate('EDIT');
        
        if($res['err'])
            $this->show_msg ('validate failed.', $res['err'], 0);               
        
        //check the record is exists.
        $book = $res['book'];        
        $where = array(
            'deleted'   => 0,
            'user_id'   => $this->user->user_id,
            'book_id'   => $book['book_id']
        );
        $item = $this->CI->m_Book->select_one($where);
        
        if(!$item)
            $this->error('recored is not found.');
        
        //update record
        $book['last_modify'] = date('Ymd H:i:s',time());        
        if($this->CI->m_Book->update($book)){
            $this->show_msg ('success');
        }
        else
        {
            $this->error('update failed');
        }                
    }
    
    /*
     * Validate data
     */
    private function validate($action='ADD'){
        $book = array(
            'book_id'   =>$this->req['id'] * 1,
            'user_id'  => $this->user->user_id,
            'username' => $this->user->username,
            'book_name' => $this->req['displayName'],
            'subject' => $this->req['subject'],
            'comment' => $this->req['comment'],
            'location' => $this->req['location'],
            'book_type' => $this->req['bookType'],
            'type_id' => $this->req['typeId'] * 1,
            'amount' => $this->req['amount'] * 1
        );
        
        $err = array();        
        if(!isset(trim($book['subject']))) $err[] = 'subject is blank.';
        if($book['type_id'] <= 0) $err[] = 'type should be selected.';
        if($book['amount'] <= 0) $err[] = 'amount should be input as number.';
        if($action =='EDIT' && $book['book_id'] <= 0) $err[] = 'record id is incorrect.';
        
        return array('book'=>$book,'err'=>$err);
    }


    /*
     * Delete a record
     * p={id:?}
     */
    public function delete()
    {        
        //validate data
        $book_id = $this->req['id'] * 1;
        if($book_id<=0) $this->error('record id is incorrect.');
        
        //check the record is exists.              
        $where = array(
            'deleted'   => 0,
            'user_id'   => $this->user->user_id,
            'book_id'   => $book_id
        );
        $item = $this->CI->m_Book->select_one($where);
        
        if(!$item)
            $this->error('recored is not found.');
        
        if($this->CI->m_Book->delete($book_id)){
            $this->show_msg ('success');
        }
        else
        {
            $this->error('delete failed');
        }    
    }
    
    /*
     * generate a (weekly/monthly) report
     */
    public function report()
    {
        $this->error('not yet impelemented.');
    }
    // --------------------------------------------------------------------

	
}